<template>
    <el-dialog
        :close-on-click-modal	="false"
        :modelValue="dialogVisible"
        @update:modelValue="(e)=>{emit('update:dialogVisible',e)}"
        title="Tips"
        width="50%"
        :before-close="handleClose"
        @open="handleOpen"
    >

        <el-form :model="formParams">
            <el-form-item v-if="type==='add' && editNode" label="上级组织机构" :label-width="formLabelWidth">
                <el-input disabled :value="editNode.title" autocomplete="off" />
            </el-form-item>
            <el-form-item label="displayName" :label-width="formLabelWidth">
                <el-input v-model="formParams.displayName" autocomplete="off" />
            </el-form-item>

        </el-form>


        <template #footer>
            <span class="dialog-footer">
                <el-button @click="emit('update:dialogVisible',false)">Cancel</el-button>
                <el-button type="primary" @click="sureClick">
                    Confirm
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from 'vue'
import {organizationUnitsCreate,organizationUnitsUpdate} from "@/api/organizationUnits"
import { ElMessage, ElMessageBox } from 'element-plus'
const props = defineProps({
  dialogVisible: {
    type:Boolean,
    default:false
  },
  editNode:{
    type:Object,
    default:function() {
      return null
    }
  },
  type:{
    type:String,
    default:'add'
  }
})
const emit = defineEmits(['update:dialogVisible','hanldeRequest']);
const formLabelWidth = '140px'
const formParams = ref({
  displayName:''
})

function handleOpen() {
  if(props.editNode) {
    if(props.type === 'add') {
      formParams.value.displayName = '';
    }else{
      formParams.value.displayName = props.editNode.title;
    }
  }else{
    formParams.value.displayName = '';
  }
}


async function sureClick() {

  if(props.type === 'add') {

    if(props.editNode) {
      await organizationUnitsCreate({...formParams.value,parentId:props.editNode.key})
    }else{
      await organizationUnitsCreate(formParams.value)
    }
    ElMessage({
      type: 'success',
      message: '新增成功',
    })

  }else if(props.type === 'edit') {
    await organizationUnitsUpdate({...formParams.value,id:props.editNode.key})
    ElMessage({
      type: 'success',
      message: '修改成功',
    })
  }
  emit('update:dialogVisible',false)
  emit('hanldeRequest')
}
</script>

<style lang="scss" scoped>

</style>